//
//  LocationView.m
//  YIJU2.0-HD
//
//  Created by 张金国 on 16/5/9.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "LocationView.h"
#import "UIColor+RandomColor.h"
#import "UIButton+ColorandText.h"
#import "TitleView.h"
#import "Masonry.h"
#import "Macro.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>

@interface LocationView ()

@property (nonatomic, strong)UIView       *sandBoxView;

@property (nonatomic, strong)MAMapView    *mapView;

@property (nonatomic, strong)UIView       *adjustmentView;

@property (nonatomic, strong)UISearchController *searchController;

@property (nonatomic ,strong)UIButton     *nearbySearchButton;

@property (nonatomic ,strong)UIView       *detailView;

@end


@implementation LocationView

#pragma mark - init
- (instancetype)init {
    if (self = [super init]) {
        [self setupConstraints];
    }
    return self;
}

#pragma mark - private
- (void)setupConstraints {
    [self.sandBoxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.equalTo(self);
        make.width.equalTo(self.mas_width).multipliedBy(1/3.0f).priorityLow(1000);
    }];
    
    [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self);
        make.left.equalTo(self.sandBoxView.mas_right);
    }];
    
    [self.searchController.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mapView.mas_top).offset(20);
        make.left.equalTo(self.mapView.mas_left).offset(20);
        make.right.equalTo(self.nearbySearchButton.mas_left).offset(-40);
        make.height.equalTo(@40);
    }];
    
    [self.nearbySearchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mapView.mas_top).offset(20);
        make.right.equalTo(self.mapView.mas_right).offset(-20);
        make.height.equalTo(@40);
        make.width.equalTo(@80);
    }];
    
    [self.detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nearbySearchButton.mas_bottom).offset(20);
        make.centerX.equalTo(self.nearbySearchButton.mas_centerX);
        make.height.equalTo(self.mapView.mas_height).multipliedBy(0);
        make.width.equalTo(@0);
    }];
    
    [self.adjustmentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.sandBoxView.mas_centerY);
        make.centerX.equalTo(self.sandBoxView.mas_right);
        make.height.equalTo(@100);
        make.width.equalTo(@40);
    }];
    
    UIButton *adjustmentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    adjustmentButton.backgroundColor = [UIColor redColor];
    [adjustmentButton addTarget:self action:@selector(adjustmentButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.adjustmentView addSubview:adjustmentButton];
    
    [adjustmentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(self.adjustmentView);
        make.width.equalTo(self.adjustmentView.mas_width).multipliedBy(1/2.0f);
    }];
    
    [self setupConstraintsOfsandBoxView];
    
    [self setupConstraintsOfDetailView];
}

- (void)setupConstraintsOfDetailView {
    
    UIButton *tempBtn = nil;
    for (int i = 0; i < 8; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i+100;
        NSString * imageName = [NSString stringWithFormat:@"icon_%d",i];
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.detailView addSubview:button];
        
        if (tempBtn == nil) {
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.detailView.mas_top);
                make.left.right.equalTo(self.detailView);
                make.height.equalTo(self.detailView.mas_height).multipliedBy(1/8.0f);
            }];
        }else {
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(tempBtn.mas_bottom);
                make.left.right.equalTo(self.detailView);
                make.height.equalTo(self.detailView.mas_height).multipliedBy(1/8.0f);
            }];
        
        }
        tempBtn = button;
    }

}


- (void)setupConstraintsOfsandBoxView {
    TitleView *titleView = [[TitleView alloc]initWithLineColor:[UIColor redColor] title:@"沙盘控制" titleFont:kLabelTextColor];
    [self.sandBoxView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sandBoxView.mas_top).offset(20);
        make.left.equalTo(self.mas_left).offset(20);
        make.width.equalTo(self.sandBoxView.mas_width).offset(1/4.0f).priorityLow(300);
        make.height.equalTo(self.sandBoxView.mas_height).multipliedBy(0.05);
    }];
}

#pragma mark - event response
- (void)adjustmentButtonEvent:(UIButton *)button {
    button.selected = !button.selected;
    if(button.selected == NO) {
        [self.sandBoxView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.equalTo(self);
            make.width.equalTo(self.mas_width).multipliedBy(1/3.0f).priority(1000);
        }];
    }else if(button.selected == YES){
        [self.sandBoxView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.equalTo(self);
            make.width.equalTo(@0);
        }];
    }
    [UIView animateWithDuration:0.3 animations:^{
             [self layoutIfNeeded];
    }];
}


- (void)nearbySearchButtonEvent:(UIButton *)button {
    button.selected = !button.selected;
    if(button.selected == NO) {
        [self.detailView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.nearbySearchButton.mas_bottom).offset(20);
            make.centerX.equalTo(self.nearbySearchButton.mas_centerX);
            make.width.equalTo(@80);
            make.height.equalTo(0).priority(1000);
        }];
    }else if(button.selected == YES){
        [self.detailView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.nearbySearchButton.mas_bottom).offset(20);
            make.centerX.equalTo(self.nearbySearchButton.mas_centerX);
            make.bottom.equalTo(self.mapView.mas_bottom);
            make.width.equalTo(@80);
        }];
    }
    [UIView animateWithDuration:0.2 animations:^{
        [self layoutIfNeeded];
    }];


}

- (void)buttonEvent:(UIButton *)button {
    NSLog(@"sfsadf");

}

#pragma mark - getter
- (UIView *)sandBoxView {
    if (!_sandBoxView) {
        _sandBoxView = [[UIView alloc]init];
        _sandBoxView.backgroundColor = [UIColor randomColor];
        [self addSubview:_sandBoxView];
    }
    return _sandBoxView;
}

- (MAMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MAMapView alloc]init];
        [self addSubview:_mapView];
    }
    return _mapView;
}

- (UIView *)adjustmentView {
    if (!_adjustmentView) {
        _adjustmentView = [[UIView alloc]init];
        _adjustmentView.backgroundColor = [UIColor blackColor];
        _adjustmentView.layer.masksToBounds = YES;
        _adjustmentView.layer.cornerRadius = 20;
        _adjustmentView.userInteractionEnabled = YES;
        [self addSubview:_adjustmentView];
    }
    return _adjustmentView;
}

- (UISearchController *)searchController {
    if (!_searchController) {
        _searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        _searchController.searchResultsUpdater = self;
        _searchController.dimsBackgroundDuringPresentation = NO;
        _searchController.hidesNavigationBarDuringPresentation = NO;
        _searchController.searchBar.frame = CGRectZero;
        [self.mapView addSubview:_searchController.searchBar];
    }
    return _searchController;
}

- (UIButton *)nearbySearchButton {
    if (!_nearbySearchButton) {
        _nearbySearchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nearbySearchButton setTitle:@"周边配套" forState:UIControlStateNormal];
        _nearbySearchButton.layer.masksToBounds = YES;
        [_nearbySearchButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _nearbySearchButton.layer.borderWidth = 1.0f;
        _nearbySearchButton.layer.borderColor = [UIColor grayColor].CGColor;
        _nearbySearchButton.layer.cornerRadius = 5.0f;
        [_nearbySearchButton addTarget:self action:@selector(nearbySearchButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.mapView addSubview:_nearbySearchButton];
    }
    return _nearbySearchButton;
}

- (UIView *)detailView {
    if (!_detailView) {
        _detailView = [[UIView alloc]init];
        _detailView.backgroundColor = [UIColor colorWithWhite:0.702 alpha:0.524];
        [self.mapView addSubview:_detailView];
    }
    return _detailView;
}


@end
